package cn.itcast.itcaststore.serive.impl;

import cn.itcast.itcaststore.dao.ProductDao;
import cn.itcast.itcaststore.dao.impl.ProductDaoImpl;
import cn.itcast.itcaststore.domain.PageBean;
import cn.itcast.itcaststore.domain.Products;
import cn.itcast.itcaststore.exception.AddProductException;
import cn.itcast.itcaststore.exception.FindProductByIdException;
import cn.itcast.itcaststore.exception.ListProductException;
import cn.itcast.itcaststore.serive.ProductService;
import java.sql.SQLException;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    private ProductDao productDao = new ProductDaoImpl();

    @Override
    public List<Object[]> download(String year, String month) {
        List<Object[]> salesList = null;
        try {
            salesList = productDao.salesList(year,month);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return salesList;
    }

    @Override
    public PageBean findProductByPage(int currentPage, int currentCount, String category) {
        //查询当前分类总的记录条数
        int totalCount = productDao.findProductTotalCount(category);
        //得到总页数
        int totalPage = (int) Math.ceil(totalCount*1.0/currentCount);
        //创建PageBean对象
        PageBean pageBean = new PageBean();
        //为PageBean的总记录条数赋值
        pageBean.setTotalCount(totalCount);
        //为PageBean的当前页赋值
        pageBean.setCurrentPage(currentPage);
        //为PageBean的总页数赋值
        pageBean.setTotalPage(totalPage);
        //为PageBean的每页条数赋值
        pageBean.setCurrentCount(currentCount);
        List<Products> products = productDao.findProductByPage(currentPage, currentCount, category);
        //为PageBean每页显示的数据赋值
        pageBean.setPs(products);
        //为PageBean的category数据赋值
        pageBean.setCategory(category);
        return pageBean;
    }

    @Override
    public Products findProductById(String id) throws FindProductByIdException {
        try {
            return productDao.findProductById(id);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new FindProductByIdException("根据ID查找商品失败");
        }
    }

    @Override
    public List<Products> findProductByManyCondition(String id, String name, String category, String minprice, String maxprice) {
        List<Products> ps = null;
        try {
            ps = productDao.findProductByManyCondition(id, name, category, minprice, maxprice);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }

    @Override
    public PageBean findBookByName(int currentPage, int currentCount, String searchfield) {
        //创建PageBean对象
        PageBean pageBean = new PageBean();

        //查询当前书名总的记录条数
        int totalCount = productDao.findTotalCountByName(searchfield);
        //得到总页数
        int totalPage = (int) Math.ceil(totalCount * 1.0 / currentCount);
        //为PageBean的总记录条数赋值
        pageBean.setTotalCount(totalCount);
        //为PageBean的当前页赋值
        pageBean.setCurrentPage(currentPage);
        //为PageBean的总页数赋值
        pageBean.setTotalPage(totalPage);
        //为PageBean的每页条数赋值
        pageBean.setCurrentCount(currentCount);
        //调用product的方法，分页显示数据
        List<Products> products = productDao.findProductByNameByPage(currentPage, currentCount, searchfield);
        //为PageBean每页显示的数据赋值
        pageBean.setPs(products);
        pageBean.setSearchfield(searchfield);
        return pageBean;
    }

    @Override
    public List<Object[]> getWeekHotProduct() {
        return productDao.getWeekHotProduct();
    }

    @Override
    public List<Products> listAll() throws ListProductException {
        try {
            return productDao.listAll();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ListProductException("查询商品失败");
        }
    }

    @Override
    public void addProduct(Products p) throws AddProductException {
        try {
            productDao.addProduct(p);
        }catch (SQLException e){
            e.printStackTrace();
            throw new AddProductException("添加商品失败");
        }
    }

    @Override
    public void editProduct(Products p) {
        try {
            productDao.editProduct(p);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteProduct(String id) {
        try {
            productDao.deleteProduct(id);
        }catch (SQLException e){
            throw new RuntimeException("后台系统根据id删除商品信息失败!");
        }
    }
}
